import { api, util, dialog } from '../../../../js/util'
import BasePage from '../../../../js/common/base-page'
import { orderdetail} from "../../../../js/modules/order.js"
BasePage({
  pageKey:"logisticsTracking",

  /**
   * 页面的初始数据
   */
  data: {
    Name: '',
    Code: '',
    Mobile:"",
    traces: [],
    isLoadComplete:false,
    id:'',
    OddNumber: '', // 发货单号
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    // var name=options.name,code=options.code;
    // if(name!=undefined&&code!=undefined){
    //   that.setData({
    //     Name:name,
    //     Code:code,
    //     Mobile:options.mobile
    //   });
    //   that.get_express();
    // }
    that.setData({
      id:options.id,
      OddNumber: options.OddNumber || ''
    })
    that.get_express();
  },
  /**重新加载 */
  onReloadEvent:function(){
    that.get_express();
  },
  /**
   * 获得快递信息
   */
  get_express(){
    var that = this;
    let OddNumber = this.data.OddNumber
    orderdetail.getLogistics(that.data.id, OddNumber, function(res){
      // res = res.Content;
      console.log(res.logisticJson,'======');
      that.setData({
        Name:res.LogisticsName,
        Code:res.LogisticsOdd,
        Mobile:res.Mobile
      })
      
      // res = res.replace(/(.*);(.*)/g, "$1$2")
      var express = res.logisticJson;
      that.setData({
        isLoadComplete:express?false:true
      });
      if (express&&express.Traces) {
        var traces = express.Traces.reverse();
        that.setData({
          traces: traces,
          isLoadComplete:traces.length==0
        });
      }
    })
  },
  // get_express:function(){
  //   var that=this;
  //   let {Code,Name,Mobile}=this.data;
  //   var sign = util.md5(Name + "|" + Code + "|ecplus");
  //   util.get(`${api.gwDomain}/bis/express?name=${Name}&code=${Code}&s=${sign}&mobile=${Mobile}`, function (res) {
  //     res = res.data;
  //     // console.log(res);
  //     if (res.indexOf("Success:false") > -1) {
  //       that.setData({
  //         traces: [],
  //         isLoadComplete:true
  //       });
  //       return;
  //     }
  //     res = res.replace(/(.*);(.*)/g, "$1$2")
  //     var express = JSON.parse(res.replace(/var\s+express\s*=\s*/, ""));
  //     if (express&&express.Traces) {
  //       var traces = express.Traces.reverse();

  //       that.setData({
  //         traces: traces,
  //         isLoadComplete: true
  //       });
  //     }

  //   });
  // },
  onClipboardEvent(){
    wx.setClipboardData({
      data: this.data.Code
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})